﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GestionTaxesRiaApp.Web;
using System.ServiceModel.DomainServices.Client;

namespace GestionTaxesRiaApp.Views
{
    public partial class ValiderFrequentation : Page
    {
        GestionTaxesContext gestionTaxesContext = new GestionTaxesContext();
        public ValiderFrequentation()
        {
            SetDroits();
            InitializeComponent();
            Frequentation frequentation = GestionFrequentation.FrequentationStatic;
            var proprietaire = gestionTaxesContext.Load<Utilisateur>(gestionTaxesContext.GetUtilisateurByIdQuery(frequentation.fre_IdProprietaire), ProprietaireLoadedCallback, false);
            var reduction = gestionTaxesContext.Load<Reduction>(gestionTaxesContext.GetReductionByIdQuery(frequentation.fre_IdReduction), ReductionLoadedCallback, false);
            var logement = gestionTaxesContext.Load<Logement>(gestionTaxesContext.GetLogementByIdQuery(frequentation.fre_IdLogement), LogementLoadedCallback, false);
            PleinTarifTb.Text = frequentation.fre_NbNuitTarifPlein.ToString();
            TarifReduitTb.Text = frequentation.fre_NbNuitTarifReduit.ToString();

        }
        void ProprietaireLoadedCallback(LoadOperation<Utilisateur> loadOperation)
        {
            Utilisateur proprietaire = loadOperation.Entities.FirstOrDefault();
            ProprietaireTB.Text = proprietaire.uti_Prenom + " " + proprietaire.uti_Nom;
            
        }
        void LogementLoadedCallback(LoadOperation<Logement> loadOperation)
        {
            Logement logement = loadOperation.Entities.FirstOrDefault();
            LogementTB.Text = logement.log_Nom;

        }
        void ReductionLoadedCallback(LoadOperation<Reduction> loadOperation)
        {
            Reduction reduction = loadOperation.Entities.FirstOrDefault();
            ReductionTB.Text = reduction.red_Libelle;

        } 

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/GestionFrequentation", UriKind.Relative));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Frequentation frequentation = GestionFrequentation.FrequentationStatic;
            frequentation.fre_IsValider = 1;
            gestionTaxesContext.SubmitChanges();
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/GestionFrequentation", UriKind.Relative));
        }
        private void SetDroits()
        {
            if (Connexion.utilisateurStatic.uti_IdFonction != Home.gestionnaireInt
                && Connexion.utilisateurStatic.uti_IdFonction != Home.administrateurInt)
            {
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/Gestions", UriKind.Relative));
            }
            if (Connexion.utilisateurStatic.uti_IdFonction == Home.decoInt)
            {
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/Connexion", UriKind.Relative));
            }
        }
        

    }
}
